package com.fzdy.dataplatform.service.impl;

import com.fzdy.dataplatform.core.dao.Dao;
import com.fzdy.dataplatform.core.service.impl.BaseServiceImpl;
import com.fzdy.dataplatform.dao.AnnouncementDao;
import com.fzdy.dataplatform.dao.UserAnnouncementDao;
import com.fzdy.dataplatform.dao.UserDao;
import com.fzdy.dataplatform.entity.Announcement;
import com.fzdy.dataplatform.entity.User;
import com.fzdy.dataplatform.entity.UserAnnouncement;
import com.fzdy.dataplatform.service.UserAnnouncementService;
import com.google.common.base.Joiner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UserAnnouncementServiceImpl extends BaseServiceImpl<UserAnnouncement> implements UserAnnouncementService {

    @Autowired
    private UserAnnouncementDao userAnnouncementDao;
    @Autowired
    private AnnouncementDao announcementDao;
    @Autowired
    private UserDao userDao;
    @Override
    protected Dao getDao() {
        return userAnnouncementDao;
    }

    @Override
    public void insertT(Integer[] sendUsers, Announcement announcement) {
        Integer id = (Integer) announcementDao.save(announcement);
        String sendto;
        if (sendUsers[0]==0){
            sendto="所有人";
            User user=new User();
            user.setStatus(1);
            user.setRoid(2);
            List<User> users = userDao.findByExampleEq(user);
            for (User u:users){
                UserAnnouncement userAnnouncement=new UserAnnouncement(u.getId(),id,1);
                userAnnouncementDao.save(userAnnouncement);
            }
        }else {
            List<String> names=new ArrayList<>();
            for (Integer ids:sendUsers){
                names.add(userDao.get(ids).getName());
                UserAnnouncement userAnnouncement=new UserAnnouncement(ids,id,1);
                userAnnouncementDao.save(userAnnouncement);
            }
            sendto=Joiner.on(",").skipNulls().join(names);
        }
        announcement.setSendto(sendto);
        announcementDao.update(announcement);
    }
}
